﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kaixin.Components.DTO;
using Kaixin.Components.XML;
using Kaixin.Components.InterFace;
using Kaixin.Components;
using System.IO;
using System.Reflection;
using System.Threading;

namespace KaixinAssistantWinForm
{
    public partial class MainWin : Form
    {

        private UserSet _us = new UserSet();
        //组件集合
        private Dictionary<string, ComponentsBase> _components = new Dictionary<string, ComponentsBase>();

        public MainWin()
        {
            InitializeComponent();
        }

        private void btnAddUser_Click(object sender, EventArgs e)
        {
            User user = new User(txtEmail.Text, txtPWD.Text);
            
            _us.Add(user);
            UserBind();
        }

        private void MainWin_Load(object sender, EventArgs e)
        {
            UserBind();
            List<Thread> threads = new List<Thread>();
            //测试创建大亨组件
            foreach (var cf in GetAllComponentFactory())
            {
                ComponentsBase component = cf.CreateComponent();
                threads.Add(component.BackgroundThread);
                _components.Add(component.Name, component);
                ToolStripMenuItem componentMainMenu = (ToolStripMenuItem)menu.Items.Add(component.Name);
                foreach (var item in component.GetMenus())
                {
                    ToolStripMenuItem menuItem = (ToolStripMenuItem)componentMainMenu.DropDownItems.Add(item);
                    menuItem.Click += new EventHandler(menuItem_Click);
                }
            }


            dgvThread.DataSource = threads;
        }

        void menuItem_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem menu = (ToolStripMenuItem)sender;
            ComponentsBase component = _components[menu.OwnerItem.Text];
            ktpBottom.Controls.Clear();
            ktpBottom.Controls.Add(component.UIFactory.CreatePanelFactory(menu.Text).CreatePanel());
        }

        private void UserBind()
        {
            kryptonDataGridView1.DataSource = _us.GetAllItem();
        }

        private void 控制首页ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ktpBottom.Controls.Clear();
            ktpBottom.Controls.Add(kpdFirst);
        }

        private void kryptonButton1_Click(object sender, EventArgs e)
        {
            TestUIPanel ui = new TestUIPanel();
            ui.Show();
           
        }


        //反射组件
        private List<IComponentsFactory> GetAllComponentFactory()
        {
            List<IComponentsFactory> list = new List<IComponentsFactory>();
            DirectoryInfo di = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory);
            foreach (var file in di.GetFiles("*.dll"))
            {
                Assembly ass = Assembly.LoadFile(file.FullName);
                foreach (var item in ass.GetTypes())
                {
                    if (typeof(IComponentsFactory).IsAssignableFrom(item) && !item.IsInterface)
                    {
                        list.Add((IComponentsFactory)Activator.CreateInstance(item));
                    }
                }
            }
            return list;
        }

        private void MainWin_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (var item in _components.Values)
            {
                try
                {
                    item.BackgroundThread.Abort();
                }
                catch (Exception)
                {
                }
            }
        }

        private void kryptonButton2_Click(object sender, EventArgs e)
        {
            string masterDLL = @"D:\My Documents\Visual Studio 2008\Projects\KaixinAssistantWinForm\MasterComponent\bin\Debug\MasterComponent.dll";
            string here = @"D:\My Documents\Visual Studio 2008\Projects\KaixinAssistantWinForm\KaixinAssistantWinForm\bin\Debug\MasterComponent.dll";

            FileInfo file = new FileInfo(masterDLL);
            file.CopyTo(here);
        }
    }
}
